package com.hejia.bulbul.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.PopularizeVisit;
import com.hejia.alauda.persistence.mybatis.PopularizeCategoryMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeVisitMapper;
import com.hejia.alauda.utils.Pager;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Service
public class PopularizeVisitServiceImpl implements PopularizeVisitService {

	private static final Log Logger = LogFactory
			.getLog(PopularizeVisitServiceImpl.class);

	@Resource
	private PopularizeVisitMapper popularizeVisitMapper;

	@Resource
	private PopularizeCategoryMapper popularizeCategoryMapper;

	@Resource
	private PopularizeMapper popularizeMapper;

	@Resource
	private MongoTemplate mongoTemplate;

	@Override
	public PopularizeVisit add(PopularizeVisit visit) {
		visit.setCreateDate(new Date());
		visit.setModifyDate(new Date());
		popularizeVisitMapper.insert(visit);
		return visit;
	}

	@Override
	public Pager<PopularizeVisit> findPopularizeVisitByPopularizeId(
			Long popularizeId, Pager<PopularizeVisit> pager) {
		
		DBObject matchCondition = new BasicDBObject("popularizeId", popularizeId);
		DBObject match = new BasicDBObject("$match", matchCondition);

		DBObject sortCondition = new BasicDBObject("createDate", -1);
		DBObject sort = new BasicDBObject("$sort", sortCondition);
		
		DBObject skip = new BasicDBObject("$skip", pager.getStartNumber());
		DBObject limit = new BasicDBObject("$limit", pager.getPageSize());
		
		List<DBObject> pipeline = Arrays.asList(match, sort, skip, limit);
		
		Logger.debug("findPopularizeVisitByPopularizeId " + pipeline.toString());
		
		AggregationOutput output = this.mongoTemplate.getCollection(
				"popularizeVisit").aggregate(pipeline);
		Iterator<DBObject> results = output.results().iterator();

		List<PopularizeVisit> popularizeVisits = new ArrayList<>();
		while (results.hasNext()) {
			
			DBObject dBObject = results.next();
			
			PopularizeVisit popularizeVisit = new PopularizeVisit();
			//popularizeVisit.setId((Long)dBObject.get("_id"));
			popularizeVisit.setBiggerCategoryId((Long)dBObject.get("biggerCategoryId"));
			popularizeVisit.setCategoryId((Long)dBObject.get("categoryId"));
			popularizeVisit.setPopularizeId((Long)dBObject.get("popularizeId"));
			popularizeVisit.setCreateDate((Date)dBObject.get("createDate"));
			popularizeVisit.setModifyDate((Date)dBObject.get("modifyDate"));
			popularizeVisit.setSource((String)dBObject.get("source"));
			popularizeVisit.setSourceId((String)dBObject.get("sourceId"));
			popularizeVisit.setVisitorId((String)dBObject.get("visitorId"));
			popularizeVisit.setVisitorIP((String)dBObject.get("visitorIP"));
			popularizeVisit.setVisitUrl((String)dBObject.get("visitUrl"));
			
			popularizeVisits.add(popularizeVisit);
		}
		
		Long totalCount = this.mongoTemplate.getCollection("popularizeVisit").count(matchCondition);
		pager.setList(popularizeVisits);
		pager.setTotalCount(totalCount.intValue());
		
		return pager;
	}
}